function bkmrksToList(bkmrks) {
  var bkmrkList = []
  recurseBkmrks(bkmrks[0], bkmrkList);
  return bkmrkList;
}

function recurseBkmrks(bkmrkNode, arr) {
  if (bkmrkNode.type === "bookmark") {
    arr.push(bkmrkNode.url);
  } else if (bkmrkNode.type === "folder") {
    for (x=0; x < bkmrkNode.children.length; x++) {
      recurseBkmrks(bkmrkNode.children[x], arr);
    }
  } else {
    console.log("Unknown bkmrknode type: " + bkmrkNode.type);
  }
}

function rdmbkmrk() {
  var bkmrksPromise = browser.bookmarks.getTree();
  bkmrksPromise.then(function(bkmrks) {
    bkmrks = bkmrksToList(bkmrks);
    var rdmUrl = "https://example.org/failure";
    rdmUrl = bkmrks[Math.floor(Math.random() * bkmrks.length)];
    browser.tabs.create({
      url: rdmUrl
    });
  }, function (error) {console.error(error);});
}

browser.browserAction.onClicked.addListener(rdmbkmrk);
